package com.xh209051017.myapplication5;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private Button mbtn;
    private RecyclerView mrecycleview;
    private MyAdapter myAdapter;

    List<listModel> list = new ArrayList<>();
    MySqlite mySqlite;
    SQLiteDatabase db;


    @SuppressLint("Range")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mbtn=findViewById(R.id.btn);
        mrecycleview=findViewById(R.id.my_recycleview);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mrecycleview.setLayoutManager(manager);
        //设置偏移量
        myAdapter = new MyAdapter(this);
        mrecycleview.setAdapter(myAdapter);
        mySqlite=new MySqlite(MainActivity.this);
        db=mySqlite.getWritableDatabase();
        Cursor cursor =db.rawQuery("select * from course_info",null);
        if(cursor.getCount()!=0){
            cursor.moveToFirst();
            for (int i = 0; i < cursor.getCount(); i++) {
                listModel model = new listModel();
                model.setName(cursor.getString(cursor.getColumnIndex("n")));
                model.setXf(cursor.getString(cursor.getColumnIndex("x")));
                model.setTime(cursor.getString(cursor.getColumnIndex("t")));
                model.setTeacher(cursor.getString(cursor.getColumnIndex("tt")));
                model.setPositon(cursor.getString(cursor.getColumnIndex("p")));
                list.add(model);
                cursor.moveToNext();
            }
            myAdapter.setData(list);
            cursor.close();
        }else {

        }
        mbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,IncreaseActivity.class));
            }
        });
    }
}